package com.zxl.always.on.display.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.wx.wheelview.widget.WheelView;
import com.zxl.always.on.display.R;
import com.zxl.always.on.display.base.BaseActivity;
import com.zxl.always.on.display.constant.ICustomAction;
import com.zxl.always.on.display.constant.ISharedPreferences;
import com.zxl.always.on.display.constant.LockerSetting;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.TimerDataHelper;
import com.zxl.always.on.display.track.MobclickAgentHelper;
import com.zxl.always.on.display.track.StatisticsConstant;
import com.zxl.always.on.display.track.StatisticsType;
import com.zxl.always.on.display.ui.adapter.WheelAdapter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/9.
 */

public class TimerSettingsActivity extends BaseActivity implements View.OnClickListener {

    private String mTmpStartHour = LockerSetting.sStartHour;

    private String mTmpStartMin = LockerSetting.sStartMin;

    private String mTmpEndHour = LockerSetting.sEndHour;

    private String mTmpEndMin = LockerSetting.sEndMin;

    private View mTabStartView, mTabEndView;

    private TextView mStartTipsView, mEndTipsView;

    private WheelView mHourView, mMinView;

    private TimerDataHelper mTimerDataHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_settings);
        mTimerDataHelper = new TimerDataHelper();
        findViewById(R.id.time_setting_dialog_cancel_btn).setOnClickListener(this);
        findViewById(R.id.time_setting_dialog_ok_btn).setOnClickListener(this);

        mTabStartView = findViewById(R.id.start);
        mTabStartView.setOnClickListener(this);
        mTabStartView.setSelected(true);

        mTabEndView = findViewById(R.id.end);
        mTabEndView.setOnClickListener(this);

        mStartTipsView = findViewById(R.id.start_time);
        mEndTipsView = findViewById(R.id.end_time);

        mHourView = findViewById(R.id.wheel_hour);
        mHourView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                if (mTabStartView.isSelected()) {
                    mTmpStartHour = o.toString();
                    mStartTipsView.setText(mTmpStartHour + ":" + mTmpStartMin);
                } else {
                    mTmpEndHour = o.toString();
                    mEndTipsView.setText(mTmpEndHour + ":" + mTmpEndMin);
                }
            }
        });
        setWheelSettings(mHourView, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        mHourView.setWheelData(mTimerDataHelper.getHourList());

        mMinView = findViewById(R.id.wheel_min);
        mMinView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                if (mTabStartView.isSelected()) {
                    mTmpStartMin = o.toString();
                    mStartTipsView.setText(mTmpStartHour + ":" + mTmpStartMin);
                } else {
                    mTmpEndMin = o.toString();
                    mEndTipsView.setText(mTmpEndHour + ":" + mTmpEndMin);
                }
            }
        });
        setWheelSettings(mMinView, Gravity.LEFT | Gravity.CENTER_VERTICAL);
        mMinView.setWheelData(mTimerDataHelper.getMinList());

        refreshData();
    }

    private void setWheelSettings(WheelView wheelview, int gravity) {
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.textColor = getResources().getColor(R.color.settings_time_setting_divider_text_color);
        style.selectedTextColor = getResources().getColor(R.color.colorAccent);
        style.textSize = 21;
        wheelview.setWheelAdapter(new WheelAdapter(this, gravity));
        wheelview.setLoop(true);
        wheelview.setStyle(style);
    }

    private void refreshData() {
        mStartTipsView.setText(mTmpStartHour + ":" + mTmpStartMin);
        mEndTipsView.setText(mTmpEndHour + ":" + mTmpEndMin);
        if (mTabStartView.isSelected()) {
            mHourView.setSelection(mTimerDataHelper.getHourList().indexOf(mTmpStartHour));
            mMinView.setSelection(mTimerDataHelper.getMinList().indexOf(mTmpStartMin));
        } else {
            mHourView.setSelection(mTimerDataHelper.getHourList().indexOf(mTmpEndHour));
            mMinView.setSelection(mTimerDataHelper.getMinList().indexOf(mTmpEndMin));
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mTabEndView && !mTabEndView.isSelected()) {
            mTabEndView.setSelected(true);
            mTabStartView.setSelected(false);
            mHourView.setSelection(mTimerDataHelper.getHourList().indexOf(mTmpEndHour));
            mMinView.setSelection(mTimerDataHelper.getMinList().indexOf(mTmpEndMin));
        } else if (v == mTabStartView && !mTabStartView.isSelected()) {
            mTabEndView.setSelected(false);
            mTabStartView.setSelected(true);
            mHourView.setSelection(mTimerDataHelper.getHourList().indexOf(mTmpStartHour));
            mMinView.setSelection(mTimerDataHelper.getMinList().indexOf(mTmpStartMin));
        } else if (v.getId() == R.id.time_setting_dialog_cancel_btn) {
            finish();
            MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.CANCEL, ClockStyle.CLOCK_IMAGE.name());
        } else if (v.getId() == R.id.time_setting_dialog_ok_btn) {
            Map<String, Object> map = new HashMap<>();
            map.put(ISharedPreferences.SETTINGS_START_HOUR, mTmpStartHour);
            map.put(ISharedPreferences.SETTINGS_START_MIN, mTmpStartMin);
            map.put(ISharedPreferences.SETTINGS_END_HOUR, mTmpEndHour);
            map.put(ISharedPreferences.SETTINGS_END_MIN, mTmpEndMin);
            LockerSetting.saveLockerSettings(map);
            sendBroadcast(new Intent(ICustomAction.ACTION_TIMER_CHANGE));
            finish();
            MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.APPLY, ClockStyle.CLOCK_IMAGE.name());
        }
    }

    public static void startPage(Activity activity) {
        activity.startActivity(new Intent(activity, TimerSettingsActivity.class));
    }

}
